package com.tanhua.server.controller;

import cn.hutool.core.util.StrUtil;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("comments")
public class QuanZiCommentController {
    @Autowired
    private QuanZiService quanZiService;

    /**
     * 查询评论列表
     *
     * @return
     */
    @GetMapping
    public PageResult queryCommentsList(@RequestParam("movementId") String publishId,
                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.quanZiService.queryCommentList(publishId, page, pageSize);
    }

    /**
     * 发表评论
     *
     * @param param
     */
    @PostMapping
    public void saveComments(@RequestBody Map<String, String> param) {
        String publishId = param.get("movementId");
        String comment = param.get("comment");
        if (StrUtil.isEmpty(comment)) {
            ErrorResult.builder().errCode("500").errMessage("评论不能为空").build();
        }
        quanZiService.saveComments(publishId, comment);
    }

    /**
     * 点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("{id}/like")
    public Long likeComment(@PathVariable("id") String publishId) {
        return quanZiService.likeComment(publishId);
    }

    /**
     * 取消点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("{id}/dislike")
    public Long disLikeComment(@PathVariable("id") String publishId) {
        return this.quanZiService.disLikeComment(publishId);
    }
}
